package test.cassandra;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.utils.UUIDs;

public class App {
	
	static int i = 1;
	
	static List<UUID> schoolids = getids(1);
	static List<UUID> classids = getids(4);
	static List<UUID> studentids = getids(8);
	static List<UUID> teacherids = getids(4);
	
	public static void main(String[] args) {
		
		Cluster cluster = Cluster.builder().addContactPoint("127.0.0.1").build();
		Session session = cluster.connect("mykeyspace");
		create_table(session);
		//一个学校
		//八个学生
		//四个老师
		//四个课程
		
		//一个学校
		UUID sid = schoolids.get(0);
		session.execute(QueryBuilder.insertInto("schools").values(
				new String[]{"id", "address", "customer_id", "date_created", "name"}
				, new Object[]{sid, "school"+gettime(), UUIDs.random(), gettime(), "name"+gettime()}));
		
		
		//四个课程
		for(int i=1;i<=4;i++) {
			session.execute(QueryBuilder.insertInto("classes").values(new String[]{"id", "class_code", "school_id", "users", "date_created", "date_from", "date_to", "description", "name", "class_type"}
				, new Object[]{classids.get(i-1), "classcode"+i, sid, null, gettime(), gettime(), gettime(), "description"+i, "name"+i, "Open"}));
		}
		
		//八个学生
		for(int i=1;i<=8;i++) {
			session.execute(QueryBuilder.insertInto("users").values(
					new String[]{"id", "classes", "date_created", "email", "first_name", "last_login", "last_name", "password", "role", "school_id", "user_name"}
					, new Object[]{studentids.get(i-1), null, gettime(), "semail"+i, "sfname"+i, gettime(), "slname"+i, "spassword"+i, "student", sid, "suname"+i}));
		}
		
		//四个老师
		for(int i=1;i<=4;i++) {
			session.execute(QueryBuilder.insertInto("users").values(
					new String[]{"id", "classes", "date_created", "email", "first_name", "last_login", "last_name", "password", "role", "school_id", "user_name"}
					, new Object[]{teacherids.get(i-1), null, gettime(), "temail"+i, "tfname"+i, gettime(), "tlname"+i, "tpassword"+i, "teacher", sid, "tuname"+i}));
		}
		
		
		
		//处理关系
		//一个课程对应一个老师对应两个学生
		for(int i=1;i<=4;i++) {
			
			//课程加入用户
			Set<UUID> ids = new HashSet<>();
			UUID tid = teacherids.get(i-1);
			ids.add(tid);
			
			//老师加入课程
			session.execute(QueryBuilder.update("users")
				       .with(QueryBuilder.add("classes", "name"+i))
				       .where(QueryBuilder.eq("id", tid)));
			
			for(int j=i*2-1;j<i*2+1;j++) {
				UUID stuid = studentids.get(j-1);
				ids.add(stuid);
				
				//学生加入课程
				session.execute(QueryBuilder.update("users")
					       .with(QueryBuilder.add("classes", "name"+i))
					       .where(QueryBuilder.eq("id", stuid)));
			}
			
			session.execute(QueryBuilder.update("classes")
				       .with(QueryBuilder.addAll("users", ids))
				       .where(QueryBuilder.eq("id", classids.get(i-1))));
			
		}
		
		session.close();
		cluster.close();
	}
	
	static void create_table(Session session) {
		String users = "CREATE TABLE users ("+
						"  id uuid,"+
						"  classes set<text>,"+
						"  date_created timestamp,"+
						"  email text,"+
						"  first_name text,"+
						"  last_login timestamp,"+
						"  last_name text,"+
						"  password text,"+
						"  role text,"+
						"  school_id uuid,"+
						"  user_name text,"+
						"  PRIMARY KEY ((id))"+
						")";
		String classes = "CREATE TABLE classes ("+
						 " id uuid,"+
						 " class_code text,"+
						 " school_id uuid,"+
						 " users set<uuid>,"+
						 " date_created timestamp,"+
						 " date_from timestamp,"+
						 " date_to timestamp,"+
						 " description text,"+
						 " name text,"+
						 " class_type text,"+
						 " PRIMARY KEY ((id))"+
						 ")";
		String schools = "CREATE TABLE schools ("+
						 " id uuid,"+
						 " address text,"+
						 " customer_id uuid,"+
						 " date_created timestamp,"+
						 " name text,"+
						 " PRIMARY KEY ((id))"+
						 ")";
		
		session.execute(users);
		session.execute(classes);
		session.execute(schools);
	}
	
	static List<UUID> getids(int n) {
		List<UUID> list = new ArrayList<>();
		for(int j=0;j<n;j++) {
			list.add(getmyuuid());
		}
		return list;
	}
	
	static long gettime() {
		return new Date().getTime();
	}
	
	static UUID getmyuuid() {
		String str = i+"";
		int len = str.length();
		for(int j=1;j<=32-len;j++) {
			str = "0"+str;
		}
		//str = "0c3123885d094f44b6705461605f0b1e";
		String str1 = str.substring(0, 8);
		String str2 = str.substring(8, 12);
		String str3 = str.substring(12, 16);
		String str4 = str.substring(16, 20);
		String str5 = str.substring(20, 32);
		i++;
		return UUID.fromString(str1+"-"+str2+"-"+str3+"-"+str4+"-"+str5);
	}

//	public static void main(String[] args) {
//		//System.out.println("0c312388-5d09-4f44-b670-5461605f0b1e".length());
//		System.out.println(getmyuuid(7));
//	}
}
